home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
clean
/
sun3.lha
/
Sun3
/
pardemos
/
sieve.icl
< prev
next >
Wrap
Text File
|
1992-08-07
|
2KB
|
63 lines
MODULE sieve;
<<
The parallel Sieve of Eratosthenes.
A pipeline of reducers (Sieve's) is created. The Sieve reducers get their
input [2,3,4,...] from the Gen process. The primes held by each sieve are
communicated to a collecting process (Primes). In a picture:
Primes <--+---------+---------+---------+ - - -
| | | |
Gen -> Sieve2 -> Sieve3 -> Sieve5 -> Sieve7 -> ...
The result of the program is a list of all primes smaller than Limit.
Strictness annotations were added because the strictness analyzer doesn't
derive strictness information from parallel functions, because that could
lead to unexpected and unwanted results.
Run the program with the show constructors option on.
>>
IMPORT deltaI;
MACRO
Limit -> 150; == All primes < Limit will be computed.
RULE
<< Primes returns the list of primes. The Gen process is started up and the
function responsible for the creation of the Sieves is called.
>>
:: Primes -> [INT];
Primes -> Sieve ({P} Gen 2);
<< Gen is responsible for the creation of the Gen reducer that sends a
stream of integers greater than n to the first Sieve.
>>
:: Gen INT -> [INT];
Gen Limit -> [];
Gen n -> [n | {I} Gen !(++ n)];
<< Sieve creates the pipeline of Sieve reducers. On each processor a Filter
process is running that filters out all incoming numbers divisible by
the prime of the sieve.
>>
:: Sieve [INT] -> [INT];
Sieve [] -> [];
Sieve [p | s] -> [p | {P} Sieve ({I} Filter s p)];
:: Filter [INT] !INT -> [INT];
Filter [] p -> [];
Filter [f | r] p -> [f | {I} Filter r p] , IF > (% f p) 0
-> Filter r p;
== The Start rule: call Primes.
:: Start -> [INT];
Start -> Primes;